package com.example.pmxt.modules.carousel;

import com.example.pmxt.common.ReturnResult;
import com.example.pmxt.domain.Carousel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.UUID;


//@Api
@RestController
@Validated
@RequestMapping("/carousels")
public class CarouselController {

    @Value("${file.location}")
    String filelocation;
    @Value("${file.path}")
    String filepath;
    private final CarouselService carouselService;

    public CarouselController(@Autowired CarouselService carouselService){
        this.carouselService = carouselService;
    }

    //全文检索
    @GetMapping("/getCarouselByInput")
//    @ApiOperation("根据输入条件进行全文检索")
    public ReturnResult getCarouselByInput(@Validated String input){
        return ReturnResult.buildSuccessResult(carouselService.selectByInput(input));
    }

    //查询全部
    @GetMapping("/getCarouselAll")
//    @ApiOperation("查询全部轮播图")
    public ReturnResult getCarouselAll(){
        return ReturnResult.buildSuccessResult(carouselService.list());
    }

    //新增轮播图
//    @PostMapping()
////    @ApiOperation("新增轮播图")
//    public ReturnResult add( @RequestBody @Validated Carousel carousel) {
//        return ReturnResult.buildSuccessResult(carouselService.insert(carousel));
//    }

    //逻辑删除轮播图
    @GetMapping("/deleteCarouselById")
//    @ApiOperation("逻辑删除指定id的轮播图")
    public ReturnResult deleteCarouselById( @Validated  Integer id) {
        return ReturnResult.buildSuccessResult(carouselService.delete(id));
    }

    //修改轮播图
    @PutMapping("/updateCarouselById")
//    @ApiOperation("修改指定id的轮播图")
    public ReturnResult updateCarouselById(MultipartFile file, @Validated  Carousel carousel ) {
        if (file.isEmpty()) {
            return ReturnResult.buildFailureResult("图片为空");
        } else {
            try {
                // 保存图片
                // 服务器文件名
                String serverFilename = UUID.randomUUID().toString().substring(0, 8) + ".jpg";
                // 创建目录
                String path = filelocation;
                System.out.println(path);
                Files.createDirectories(Paths.get(path));
                // 保存
                file.transferTo(Path.of(path, serverFilename));

                // 记录上传图片url
                carousel.setCover("download/"  + serverFilename);

                return ReturnResult.buildSuccessResult(carouselService.update(carousel));
            } catch (Exception e) {
                e.printStackTrace();
                return ReturnResult.buildFailureResult(e.toString());
            }
        }
    }

    //上传图片
    @PostMapping("/addCarousel")
    public ReturnResult addCarousel(   MultipartFile file, @Validated  Carousel carousel) {
        if (file.isEmpty()) {
            return ReturnResult.buildFailureResult("图片为空");
        } else {
            try {
                // 保存图片
                // 服务器文件名
                String serverFilename = UUID.randomUUID().toString().substring(0, 8) + ".jpg";
                // 创建目录、存储图片地址目录
                String path = filelocation;
                System.out.println(path);
                // 存储图片的url
                Files.createDirectories(Paths.get(path));
                // 保存
                file.transferTo(Path.of(path, serverFilename));

                // 记录上传图片url
                carousel.setCover("download/"  + serverFilename);


                return ReturnResult.buildSuccessResult(carouselService.insert(carousel));
//                return ReturnResult.buildSuccessResult("导入成功", true);
            } catch (Exception e) {
                e.printStackTrace();
                return ReturnResult.buildFailureResult(e.toString());
            }
        }
    }

}


